#include "misc.h"
#include "driver.h"
#include "function.h"

const uint8_t dtb[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};

static uint8_t b1h;
static uint8_t b1t;
static uint8_t b1u;

static uint8_t b2h;
static uint8_t b2t;
static uint8_t b2u;

static uint8_t b3h;
static uint8_t b3t;
static uint8_t b3u;

static uint8_t b4h;
static uint8_t b4t;
static uint8_t b4u;

void teston(void)
{
	sendcmd(0x48,0x01); //set brightness, 8 seg, open light
	sendcmd1(0x48,0x01);
	sendcmd2(0x48,0x01);
	sendcmd3(0x48,0x01);

	sendcmd(0x68,0xFF);
	b1h=0xff;
	sendcmd1(0x68,0xFF);
	b2h=0xff;
	sendcmd2(0x68,0xFF);
	b3h=0xff;
	sendcmd3(0x68,0xFF);
	b4h=0xff;

	sendcmd(0x6A,0xFF);
	b1t=0xff;
	sendcmd1(0x6A,0xFF);
	b2t=0xff;
	sendcmd2(0x6A,0xFF);
	b3t=0xff;
	sendcmd3(0x6A,0xFF);
	b4t=0xff;

	sendcmd(0x6C,0xFF);
	b1u=0xff;
	sendcmd1(0x6C,0xFF);
	b2u=0xff;
	sendcmd2(0x6C,0xFF);
	b3u=0xff;
	sendcmd3(0x6C,0xFF);
	b4u=0xff;
}

void testoff(void)
{
	sendcmd(0x68,0x00);
	b1h=0x0;
	sendcmd1(0x68,0x00);
	b2h=0x0;
	sendcmd2(0x68,0x00);
	b3h=0x0;
	sendcmd3(0x68,0x00);
	b4h=0x0;

	sendcmd(0x6A,0x00);
	b1t=0x0;
	sendcmd1(0x6A,0x00);
	b2t=0x0;
	sendcmd2(0x6A,0x00);
	b3t=0x0;
	sendcmd3(0x6A,0x00);
	b4t=0x0;

	sendcmd(0x6C,0x00);
	b1u=0x0;
	sendcmd1(0x6C,0x00);
	b2u=0x0;
	sendcmd2(0x6C,0x00);
	b3u=0x0;
	sendcmd3(0x6C,0x00);
	b4u=0x0;
}

void writebank1(uint8_t hund, uint8_t tens, uint8_t unit)
{
	GIE = 0;
	if (hund != b1h){
		sendcmd(0x68,hund);
		b1h = hund;
	}
	if (tens != b1t){
		sendcmd(0x6A,tens);
		b1t = tens;
	}
	if (unit != b1u){
		sendcmd(0x6C,unit);
		b1u = unit;
	}
	GIE = 1;
}

void writebank2(uint8_t hund, uint8_t tens, uint8_t unit)
{
	GIE = 0;
	if (hund != b2h){
		sendcmd1(0x68,hund);
		b2h = hund;
	}
	if (tens != b2t){
		sendcmd1(0x6A,tens);
		b2t = tens;
	}
	if (unit != b2u){
		sendcmd1(0x6C,unit);
		b2u = unit;
	}
	GIE = 1;
}

void writebank3(uint8_t hund, uint8_t tens, uint8_t unit)
{
	GIE = 0;
	if (hund != b3h){
		sendcmd2(0x68,hund);
		b3h = hund;
	}
	if (tens != b3t){
		sendcmd2(0x6A,tens);
		b3t = tens;
	}
	if (unit != b3u){
		sendcmd2(0x6C,unit);
		b3u = unit;
	}
	GIE = 1;
}

void writebank4(uint8_t hund, uint8_t tens, uint8_t unit)
{
	GIE = 0;
	if (hund != b4h){
		sendcmd3(0x68,hund);
		b4h = hund;
	}
	if (tens != b4t){
		sendcmd3(0x6A,tens);
		b4t = tens;
	}
	if (unit != b4u){
		sendcmd3(0x6C,unit);
		b4u = unit;
	}
	GIE = 1;
}

void showNumOnBank(uint8_t bcode, uint16_t bnum)
{
	uint8_t ones, tens, pers;

	M16F = bnum;
	axu_div16d8(10);
	ones = MDXF;
	axu_div16d8(10);
	tens = MDXF;
	axu_div16d8(10);
	pers = MDXF;

	ones = dtb[ones];
	tens = dtb[tens];
	pers = dtb[pers];
	
#if HighZeroFade
	if (0x3f == pers){
		pers = 0;
		if (0x3f == tens){
			tens = 0;
		}
	}
#endif
	switch(bcode)
	{
	case TEMP_TOP:
		writebank1(pers, tens, ones);
		break;
	case PRSR_TOP:
		writebank2(pers, tens, ones);
		break;
	case TEMP_BOT:
		writebank3(pers, tens, ones);
		break;
	case PRSR_BOT:
		writebank4(pers, tens, ones);
		break;
	default:break;
	}
}

void initbankio(void)
{
	static uint8_t pda;
	static uint8_t pdb;
	static uint8_t ppa;
	static uint8_t ppb;

	pda = 0xFF;
	pdb = 0xFF;
	scl1_dir_out(pda);
	sda1_dir_out(pda);
	scl2_dir_out(pda);
	sda2_dir_out(pda);
	scl3_dir_out(pda);
	sda3_dir_out(pda);
	swky_dir_in(pda);

	scl4_dir_out(pdb);
	sda4_dir_out(pdb);
	syrx_dir_in(pdb);
	sytx_dir_out(pdb);

	set_dir_pa(pda); //set pa dir
	set_dir_pb(pdb); //set pb dir

	ppa = 0;
	ppb = 0;
	swky_pull(ppa);
	set_pullup_pa(ppa); //set pa pull up
	syrx_pull(ppb);
	set_pullup_pb(ppb); //set pb pull up

	LATA = 0xFF;
	LATB3 = 1;
	LATB4 = 1;
}